<?php

// 本类由系统自动生成，仅供测试用途

class ManageAction extends CommonAction {



public function index()

    {

    	$st=M()

    	->query('SELECT * FROM xp_se_admin a LEFT JOIN xp_se_role r ON a.adm_role=r.rol_id where a.adm_id='.$_SESSION[C('USER_AUTH_KEY')]);

    	if(count($st)==1)$this->assign('adm',$st[0]);
		
		$count=count($st);
		$this->assign('count',intval($count));


		$this->assign("big_type",M('protype')->where("type_valid=1")->order("type_seq desc")->select())

		->display();



    }
public function adr()
{
	$tip='[ <A HREF="javascript:history.back()">返 回</A> ]';
	if($_POST["rol_name"]=="")
	{
		exit("请输入权限名称！".$tip);
	}
	else
	{
		$type=M("role");
		if($type->create()) 
				{
					if(is_numeric($_POST['rol_id']))
					$ret=$type->save();
					else $ret=$type->add();

					if(false !== $ret) 

					{

		    			$this->success('数据保存成功！');

		            }else

		            {

		                exit('数据写入错误:'.$type->getLastSql()."<br/>".$tip);

		            }

				}

				else

				{

					exit($type->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');

				}
	}
}

public function adt()
{
	$tip='[ <A HREF="javascript:history.back()">返 回</A> ]';
	if($_POST["type_name"]=="")
	{
		exit("请输入分类名称！".$tip);
	}
	else
	{
		$type=M("protype");
		if($type->create()) 
				{

					$ret=$type->add();

				

					if(false !== $ret) 

					{

		    			$this->success('数据添加成功！');

		            }else

		            {

		                exit('数据写入错误:'.$type->getLastSql()."<br/>".$tip);

		            }

				}

				else

				{

					exit($type->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');

				}
	}
}

public function role()
{
	    $st= M("role");

		import("ORG.Util.Page");
		$count=$st->where("rol_id>=".sr())->count();
		$this->assign('count',intval($count));
		$Page = new Page($count,6);

		$show = $Page->show();

		$this->assign('page',$show);//赋值分页输出

		$this->assign('skip',$Page->firstRow);

	$this->assign("list",$st->where("rol_id>=".sr())->limit($Page->firstRow.','.$Page->listRows)->select())

		->display();
}
public function type()
{
	    $st= M("protype");

		import("ORG.Util.Page");
		$count=$st->count();
		$this->assign('count',intval($count));
		$Page = new Page($count,6);

		$show = $Page->show();

		$this->assign('page',$show);//赋值分页输出

		$this->assign('skip',$Page->firstRow);

	$this->assign("list",$st->order("type_valid desc,type_seq desc")->limit($Page->firstRow.','.$Page->listRows)->select())

		->display();
}

public function allc()

{



    	$st= M();

		import("ORG.Util.Page");

		$arr=$st->query('SELECT count(*) a FROM xp_se_order GROUP BY cus_name,cus_phone,order_status');

		$count= $arr[0]['a'];


		$this->assign('count',intval($count));

		$Page = new Page($count,8);

		$show = $Page->show();

		$this->assign('page',$show);//赋值分页输出

		$list = $st->query('SELECT * from xp_se_order GROUP BY cus_name,cus_phone,order_status'.' limit '.$Page->firstRow.','.$Page->listRows);

		$this->assign('list',$list);//赋值数据集

		//echo $st->getLastSql();





		$this->display();//输出模板

    	// $this->assign("allp",)

    	// ->display();

}

public function index1()

    {

    	$this->assign("big_type",M('protype')->select())->display();

    }
    public function move()
    {
    		$st = M("protype"); 


    		if(is_numeric($_POST['type_id']) && is_numeric($_POST['data']))
			{
				if($_POST['moveup']==1)
				{
					$nseq=$_POST['data']+1;
				}
				else if($_POST['moveup']==0)
				{
					$nseq=$_POST['data']-1;
				}
				else exit("Wrong");
				
				if($nseq<0)exit("已经排在最后了！");;

				
				$tp=$st->where("type_seq=".$nseq)->find();
				//exit(var_dump($_POST)+var_dump($nseq)+M()->getLastSql());
				if(!$tp)
					{
						if($_POST['moveup']==1)
							exit("已经排在第一了！");
							else exit("已经排在最后了！");

					}
				else
				{
					$data1= array('type_seq' => $nseq);
					$data2= array('type_seq' => $_POST['data']);

					if($st->where("type_id=".$_POST['type_id'])->setField($data1)!==false 
						&& $st->where("type_id=".$tp["type_id"])->setField($data2)!==false)
					exit("OK");
					else exit("Wrong");

				}

			
			}


    }
    public function check()

    {

    	if(is_numeric($_POST['value']))

    	{

    		$User = M("order"); 

    		$d=1;

    		if(is_numeric($_POST['data']))$d=$_POST['data'];

    		$data = array('order_status'=>$d);

    		if($User->where('order_id='.$_POST['value'])->setField($data)!==false)

    			{

    				exit('OK');

    			}

    			else

    			{

    				exit($User->getLastSql());

    			}

    	}elseif(is_numeric($_POST['pvalue']))

    	{

    		$User = M("protype"); 

    		$d=1;

    		if(is_numeric($_POST['data']))$d=$_POST['data'];

    		$data = array('type_valid'=>$d);

    		if($User->where('type_id='.$_POST['pvalue'])->setField($data)!==false)

    			{

    				exit('OK');

    			}

    			else

    			{

    				exit($User->getLastSql());

    			}

    	}elseif(is_numeric($_POST['rvalue']))

    	{

    		$User = M("role"); 

    		if(is_numeric($_POST['data']))$d=$_POST['data'];

    		$data = array('rol_valid'=>$d);

    		if(sr()<intval($_POST['rvalue']) && $User->where('rol_id='.$_POST['rvalue'])->setField($data)!==false)

    			{

    				exit('OK');

    			}

    			else

    			{

    				exit($User->getLastSql());

    			}

    	} 	elseif(is_numeric($_POST['pdvalue']))
    	{

    		$User = M("product"); 

    		if(is_numeric($_POST['data']))$d=$_POST['data'];

    		$data = array('pro_valid'=>$d);

    		if($User->where('pro_id='.$_POST['pdvalue'])->setField($data)!==false)

    			{

    				exit('OK');

    			}

    			else

    			{

    				exit($User->getLastSql());

    			}

    	}

    	exit('Wrong');

    }

    public function allp()

    {
    	$d=array();
    	foreach ($_GET as $key => $value) {
    		if($key!='_URL_')$d[$key]=$value;
    	}
        $this->assign("rurl",$d);

        $where="";

   		 if(isset($_GET['keywords']) && trim($_GET['keywords'])!="")
   		 {
   		 	$val=my_filter(trim($_GET['keywords']));
   		 	$where.=" where p.pro_id like '%".$val."%' or ";
   		 	$where.="p.pro_name like '%".$val."%' or ";
   			$where.="t.type_name like '%".$val."%' ";
   		 }

    	$st= M();//->query('SELECT * FROM xp_se_product p LEFT JOIN xp_se_protype t ON p.type_id=t.type_id');



		import("ORG.Util.Page");

		$arr=$st->query('select count(*) a from xp_se_product p LEFT JOIN xp_se_protype t ON p.type_id=t.type_id '.$where);

		$count= $arr[0]['a'];
		$this->assign('count',intval($count));

		$Page = new Page($count,8);

		$show = $Page->show();



		$list = $st->query('SELECT * FROM xp_se_product p LEFT JOIN xp_se_protype t ON p.type_id=t.type_id '.$where.' order by  p.pro_valid desc,p.pro_time desc limit '.$Page->firstRow.','.$Page->listRows);

		$this->assign('list',$list);//赋值数据集

		//echo $st->getLastSql();

		$this->assign('page',$show);//赋值分页输出



		$this->display();//输出模板

    	// $this->assign("allp",)

    	// ->display();

    }

    public function allo()

    {

    	$where='';

    	if(isset($_GET['order_status']))

    	{

    		if($where!='')$where.=' and ';

    		$where.='o.order_status='.$_GET['order_status'];

    	}

    	if(isset($_GET['type']))

    	{

    		if($where!='')$where.=' and ';

    		$where.='p.type_id='.$_GET['type'];

    	}

    	if(isset($_GET['cus_name']))

    	{

    		if($where!='')$where.=' and ';

    		$where.='cus_name='."'".$_GET['cus_name']."'";

    	}

    	if(isset($_GET['cus_phone']))

    	{

    		if($where!='')$where.=' and ';

    		$where.='cus_phone='."'".$_GET['cus_phone']."'";

    	}

    	if($where!='')$where=' where '.$where." ";

    	$st= M();

		import("ORG.Util.Page");

		$arr=$st->query('select count(*) a from xp_se_order o'.$where);

		$count= $arr[0]['a'];

		
		$this->assign('count',intval($count));

		$Page = new Page($count,8);

		$show = $Page->show();



		$list = $st->query('SELECT * FROM xp_se_order o LEFT JOIN xp_se_product p ON o.product_id=p.pro_id '.$where.'order by o.order_status, o.order_time desc limit '.$Page->firstRow.','.$Page->listRows);

		$this->assign('list',$list);//赋值数据集

		//echo $st->getLastSql();

		$this->assign('page',$show);//赋值分页输出



		$this->display();//输出模板

    	// $this->assign("allp",)

    	// ->display();

    }

    public function del()

    {

    	if(isset($_GET['cus_name']) && isset($_GET['cus_phone']))

    	{

    		$res = M("order")->where('cus_name='."'".$_GET["cus_name"]."'".'and cus_phone='."'".$_GET["cus_phone"]."'")->delete(); 

			if($res>0){

				$this->success("客户订单删除成功!","__URL__/allc");

			}else{

				$this->error("客户订单删除失败");

			}

    	}else if(is_numeric($_GET["pro_id"]))

    	{

    		$res = M("product")->where('pro_id='.$_GET["pro_id"])->delete(); 

			if($res>0){

				$this->success("商品删除成功!","__URL__/allp");

			}else{

				$this->error("商品删除失败");

			}

		}else if(is_numeric($_GET["order_id"]))

    	{

    		$res = M("order")->where('order_id='.$_GET["order_id"])->delete(); 

			if($res>0){

				$this->success("订单删除成功!","__URL__/allo");

			}else{

				$this->error("订单删除失败！");

			}

		}else if(is_numeric($_GET["type_id"]))

    	{

    		$res = M("protype")->where('type_id='.$_GET["type_id"]);
    		$seq=$res->find();
    		$seq=$seq["type_seq"];
    		$res ->delete(); 

			if($res>0){
				M()->execute("update xp_se_protype set type_seq=type_seq-1 where type_seq>".$seq);
				$this->success("大类删除成功!","__URL__/type");

			}else{

				$this->error("大类删除失败");

			}

		}

		else

		{

			$this->error("非法操作！");

		}	

    }

    public function newp()

    {

    	if(isset($_POST['pro_name']))
	    {


	    	$tip='[ <A HREF="javascript:history.back()">返 回</A> ]';

		   if($_POST['type_id']=='0'){

			exit('商品类别没有选择！'.$tip);

		   }

		   if(empty($_POST['pro_name'])){

		    

			exit('商品名不能为空！'.$tip);

		   }

		   if(!is_numeric($_POST['pro_inprice'])){

		   	exit('进货价格需要为数字！'.$tip);

		   }

		   if(!is_numeric($_POST['pro_shuliang'])){

		    

			exit('进货数量需要为数字！'.$tip);

		   }

		   	if(empty($_POST['pro_img'])){

		    

			exit('商品图片没有上传！'.$tip);

		   }

		   if(!is_numeric($_POST['pro_selprice'])){

		    

			exit('卖出价格需要为数字！'.$tip);

		   }

		   if(!is_numeric($_POST['pro_batprice'])){

		    

			exit('优惠价格需要为数字！'.$tip);

		   }
		$_POST["pro_time"]= date('Y-m-d H:i:s',time());
		$_POST['pro_opadmin']=$_SESSION[C('USER_AUTH_KEY')];
		$product = D("Product");

				if($product->create()) 

				{

					$ret=false;

					if(isset($_POST['pro_id']))

					{

						$ret=$product->save();

					}else

					{

						$ret=$product->add();

					}

						if(false !== $ret) 

						{

			    			$this->success('数据保存成功！');

			            }else

			            {

			            	

			                exit('数据写入错误:'.$product->getLastSql()."<br/>".$tip);

			            }

				}

				else

				{

					exit($product->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');

				}

			}

	   else

	   {


    	$d=array();
    	foreach ($_GET as $key => $value) {
    		if($key!='_URL_')$d[$key]=$value;
    	}
        $this->assign("rurl",$d);

		$this->assign("big_type",M('protype')->order('type_valid desc,type_seq desc')->select());

			if(isset($_GET['pro_id']))

			{

				$this->assign("p",M('product')->where('pro_id='.$_GET['pro_id'])->find());

			}

		$this->display();

		}

    }





// 上传图片操作

	public function UploadImg()

	{

		import('ORG.Util.UploadFile');

		$upload = new UploadFile();

        //设置上传文件大小

        $upload->maxSize= 3292200;

        //设置上传文件类型

        $upload->allowExts= explode(',', 'jpg,gif,png,jpeg');

        //设置附件上传目录

        $upload->savePath= './Index/Public/Imgs/Products/';

		

        //设置需要生成缩略图，仅对图像文件有效

        $upload->thumb= true;

        // 设置引用图片类库包路径

        $upload->imageClassPath= 'ORG.Util.Image';

        //设置需要生成缩略图的文件后缀

        $upload->thumbPrefix= 'm_,s_';  //生产2张缩略图

        //设置缩略图最大宽度

        $upload->thumbMaxWidth= '800,100';

        //设置缩略图最大高度

        $upload->thumbMaxHeight= '700,100';

        //设置上传文件规则

        $upload->saveRule= 'uniqid';

        //删除原图

        $upload->thumbRemoveOrigin  = true;

		if (!$upload->upload()) 

		{            

			echo $upload->getErrorMsg();

        } 

		else 

		{

            //取得成功上传的文件信息

            $uploadList = $upload->getUploadFileInfo();

            import('ORG.Util.Image');

			echo $uploadList[0]['savename'];

        }

	}
	public function tsql(){

		if(isset($_POST['sql']))
		{
			
			$sql=stripcslashes($_POST['sql']);
			$this->assign('sql',$sql)
			->assign('rs',M()->query($sql));
		}
			
		$this->display();
    }	

}